//
//  SuperBellViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/5/9.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class SuperBellViewController: BaseViewController, UIScrollViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource,UIAlertViewDelegate {
    
    var scrollView: UIScrollView!
    var collectionView: UICollectionView!
    var pageControl: UIPageControl!
    
    var rightArray = NSMutableArray()
    
    let RightCollectionViewCellIdentifier = "RightCollectionViewCellIdentifier"
    
    var heightScroll : CGFloat!
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "注销", style: UIBarButtonItemStyle.plain, target: self, action: #selector(SuperBellViewController.loginOut))
        self.navigationItem.setNewTitle(appname)
        
        
        InitViews()
    }
    
    func loginOut() {
        let alert: UIAlertView!
        alert = UIAlertView.init(title: "提示", message: "是否注销当前用户？", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "注销")
        alert.show()
    }
    
    func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {
        
        if buttonIndex == 1 {
            
            let defaults = UserDefaults.standard
            
            defaults.set("", forKey: "userphone")
            defaults.set("", forKey: "password")
            defaults.set("", forKey: "UpperCode")
            defaults.set("", forKey: "SupperType")
            defaults.synchronize()
            
            let myDelegate: AppDelegate = UIApplication.shared.delegate as! AppDelegate
            myDelegate.gotoLogin()
            
        }
        
        
    }
    func InitViews() {

        var view: UIView!
        view = UIView.init(frame: CGRect(x: 0, y: 64, width: screenW, height: screenH/3 - 49))
        view.backgroundColor = UIColor.init(hexString: "f7f7f7", alpha: 1)
        view.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        view.layer.shadowRadius = 2.0
        view.layer.shadowColor = UIColor.lightGray.cgColor
        view.layer.shadowOpacity = 0.8
        
        
        let imageArray = ["main1.png", "main2.png", "main3.png"]
        var picView: DCPicScrollView!
        picView = DCPicScrollView.init(frame: CGRect(x: 0, y: 0, width: screenW, height: screenH/3 - 49), withImageNames: imageArray)
        picView.titleData = nil
        picView.style = PageControlStyle.atCenter
        picView.backgroundColor = UIColor.clear
        picView.autoScrollDelay = 10.0
        view.addSubview(picView)

        
        let count = 12/13 + 1
        print("模块页" + String(count))
        scrollView = UIScrollView.init(frame: CGRect(x: 0, y: view.frame.maxY, width: screenW, height: screenH*2/3 - 35))
        scrollView.backgroundColor = UIColor.white
        scrollView.tag = 101
        scrollView.contentSize = CGSize(width: CGFloat(Float(count)) * screenW, height: 0)
        scrollView.isPagingEnabled = true;
        scrollView.showsVerticalScrollIndicator = false;
        scrollView.showsHorizontalScrollIndicator = false;
        scrollView.delegate = self
        
        
        heightScroll = scrollView.frame.height
        //        self.view.addSubview(scrollView)
        
        var fx: CGFloat = 0.0
        for i in 0...count - 1 {
            
            collectionView = UICollectionView.init(frame: CGRect(x: fx, y: 0, width: screenW , height: scrollView.frame.height), collectionViewLayout: RightCollectionFlowLayout.init())
            collectionView.tag = i
            collectionView.backgroundColor = UIColor.clear
            collectionView.register(RightCollectionViewCell.self, forCellWithReuseIdentifier: RightCollectionViewCellIdentifier)
            collectionView.scrollsToTop = true
            collectionView.delegate = self
            collectionView.dataSource = self
            scrollView.addSubview(collectionView)
            fx += screenW
            
        }
        
        pageControl = UIPageControl.init(frame: CGRect(x: 0, y: scrollView.frame.maxY, width: screenW, height: 20))
        pageControl.backgroundColor = UIColor.clear
        pageControl.tag = 201
        pageControl.numberOfPages = count
        pageControl.currentPageIndicatorTintColor = barColor
        pageControl.pageIndicatorTintColor = UIColor.init(hexString: "e2e2e2", alpha: 1)
        self.view.addSubview(pageControl)
        
        self.view.addSubview(view)
        self.view.addSubview(scrollView)

    }
    /**
     scrollview的滚动
     
     - parameter scrollView:
     */
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        let current = (scrollView.contentOffset.x)/screenW
        pageControl.currentPage = Int(current)
        
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: RightCollectionViewCell = self.collectionView.dequeueReusableCell(withReuseIdentifier: RightCollectionViewCellIdentifier, for: indexPath) as! RightCollectionViewCell
        cell.backgroundColor = UIColor.white
        
        let tag = collectionView.tag
        let count = (indexPath as NSIndexPath).row + tag*12
        
        if count >= 12 {
            cell.icon.image = UIImage.init(named: "")
            cell.title.text = ""
        }else {
            switch (indexPath as NSIndexPath).row {
            case 0:
                cell.icon.image = UIImage.init(named: "tongji")
                cell.title.text = "数据统计"
                break
            case 1:
                cell.icon.image = UIImage.init(named: "chenjichaxun")
                cell.title.text = "访问量排行"
                break
            case 2:
                cell.icon.image = UIImage.init(named: "tiaocha")
                cell.title.text = "活跃度排行"
                break
            case 3:
                cell.icon.image = UIImage.init(named: "guanzu")
                cell.title.text = "学校详情"
                break
            default:
                cell.icon.image = UIImage.init(named: "")
                cell.title.text = ""
                break
            }
        }
        
        return cell;
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let tag = collectionView.tag
        let count = (indexPath as NSIndexPath).row + tag*12
        
        if count >= 12 {
            
        }else {
            switch (indexPath as NSIndexPath).row {
            case 0:
                //数据统计
                let dataDetail = DataDetailViewController()
                self.navigationController?.pushViewController(dataDetail, animated: true)
                break
            case 1:
                //访问量排行
                let traffic = TrafficViewController()
                self.navigationController?.pushViewController(traffic, animated: true)
                break
            case 2:
                //活跃度排行
                let active = ActiveViewController()
                self.navigationController?.pushViewController(active, animated: true)
                break
            case 3:
                /// 学校详情
                let schstatic = SchStatisViewController()
                self.navigationController?.pushViewController(schstatic, animated: true)
                break
            default:
                
                break
            }
            
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}
